package com.cz.ssyx.order.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.ssyx.common.auth.AuthContextHolder;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.order.OrderInfo;
import com.cz.ssyx.order.service.OrderInfoService;
import com.cz.ssyx.vo.order.OrderConfirmVo;
import com.cz.ssyx.vo.order.OrderSubmitVo;
import com.cz.ssyx.vo.order.OrderUserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Joker
 * @since 2024-05-04
 */
@RestController
@RequestMapping("api/order")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

     //查询订单
    @GetMapping("auth/findUserOrderPage/{page}/{limit}")
    public Result findUserOrderPage(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    OrderUserQueryVo orderConfirmVo) {
        Long userId = AuthContextHolder.getUserId();
        orderConfirmVo.setUserId(userId);
        Page<OrderInfo> orderInfoPage = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderInfoService.findUserOrderPage(orderInfoPage, orderConfirmVo);
        return Result.ok(pageModel);
    }

    //确认订单
    @GetMapping("auth/confirmOrder")
    public Result confirm() {
        OrderConfirmVo orderConfirmVo = orderInfoService.confirmOrder();
        return Result.ok(orderConfirmVo);
    }

    //生成订单
    @PostMapping("auth/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitVo orderSubmitVo) {
        Long userId = AuthContextHolder.getUserId();
        Long orderId = orderInfoService.submitOrder(orderSubmitVo,userId);
        return Result.ok(orderId);
    }

    //获取订单详情
    @GetMapping("auth/getOrderInfoById/{orderId}")
    public Result getOrderInfoById(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderInfoService.getOrderInfoById(orderId);
        return Result.ok(orderInfo);
    }
}


